package tools.yale;

import java.io.File;
import java.util.Iterator;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;



public class yaleClusterXML2CSV {
	public static void main(String[] args)throws Exception{
		try{
			toCSV(args[0]);
		}catch (Exception e) {
			System.out.println(printHelp());
			System.exit(616);
		}
		
		System.exit(0);
	}
	
	private static String printHelp(){
		String help="converts a yale agglomeric cluster file (not the hierarchial one) to a csv-file\n";
		help+="input=<infile>\n";
		
		return help;
	}

	private static void toCSV(String infile)throws Exception{
		Document d=new SAXBuilder().build(new File(infile));
		String cluster_name;
		System.out.println("id\tcluster");
		for (Iterator iter = d.getRootElement().getChild("cluster").getChildren("cluster").iterator(); iter.hasNext();) {
			Element cluster = (Element) iter.next();
			cluster_name=cluster.getAttributeValue("id");
			for (Iterator iterator = cluster.getChildren("object").iterator(); iterator.hasNext();) {
				Element data = (Element) iterator.next();
				System.out.println(data.getAttributeValue("id")+"\t"+cluster_name);
			}
		}
	}
}
